tibasicfandomcom-20200215-history
Quadratic Formula
One of the main purposes of programs is to simplify often-performed tasks. In mathematics, these tasks often take the form of common and important equations---for example, the quadratic formula, used to find the roots or "zeroes" of a quadratic equation---the points where it crosses the x-axis. This article will show how to construct a TI-BASIC program that will do the quadratic formula, while illustrating a few of the program's key points. The formula is higher-level algebra, but this article won't go too in-depth mathematically. The Equation The quadratic formula is as follows: If a, b, and c are all constants, and ax^2+bx+c=0,\,\! then x = \frac{-b \pm \sqrt {b^2-4ac}}{2a}. Getting Information The first thing we need to do is to ask the user for a, b, and c. Something like this: :Input A :Input B :Input C would work, but doesn't it seem a bit unnecessary to do the same thing three times? We can simplify the programming for ourselves. :Prompt A,B,C will do the same thing in just one line of code. This illustrates an important rule of thumb for programming: in general, a program should not contain duplicate or very similar pieces of code. In this case, we're lucky because TI-BASIC provides a way to combine multiple Input commands, but you should always be looking for ways to combine parts of your programs. Performing the Calculation Now all we need to do is calculate and display the results. Here's the quadratic formula in calculator terms: :Disp (-B+√(B^2-4AC))/(2A) :Disp (-B-√(B^2-4AC))/(2A) That will store and print the results in the variables X and Y (the "plus-or-minus" sign in the formula means that there are two results). Similar to the way we input numbers in the first step, TI-BASIC allows us to display to values without writing the same line twice. This code may be re-written as: :Disp (-B+√(B^2-4AC))/(2A),(-B-√(B^2-4AC))/(2A) For more complicated programs, a While or For Loop may be beneficial, but in this case is cumbersome. Testing Now the program should be ready. Go ahead and try it out with A = 1, B = 1, and C = -6. You should get solutions of 2 and -3. Let's try it out with A = 2, B = 1, and C = 3. You should get output like the following (if your calculator is in REAL mode and not a+bi mode): ERROR: NONREAL ANSWERS Whoops! What does that mean? Take a look at the "discriminant" of the quadratic formula (the part under the radical, that we take the square root of): b^2-4ac In our situation, that would be: 1^2-4(2)(3)=-23 The problem with this is that the square root of a negative number is nonreal, or imaginary. In standard mode, calculator tries to avoid nonreal answers, so anything containing an imaginary number is considered an error. Now, you might just say, "Well, I'm not going to be dealing with imaginary numbers," but the problem is that you want your program to be as useful as possible. Maybe you'll be using imaginary numbers at some point, and then you'll want your program to take them into account. This is the simple fix to the problem: # Go back to editing your program. # Make a new line at the very beginning. # Press MODE # Press ENTER on a+bi. The whole program should now look like this: PROGRAM:QUADFORM :a+bi :Prompt A,B,C :Disp(-B+√(B^2-4AC))/(2A),(-B-√(B^2-4AC))/(2A) And you have a complete program! Another possibility for the program takes advantage of list behavior: PROGRAM:QUADFORM :a+bi :Prompt A,B,C :Disp (-B+{-1,1}√(B^2-4AC))/(2A) This will return a two-entry list containing the two answers. Category:Programing Examples